package com.earogya.communicate;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.async.Callback;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMDocument;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.soap.SOAPHeaderBlock;

public class Communicator {

	public String sendMessage(String message) throws AxisFault {
		String reply = "No reply";
		try {
			SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
			SOAPConnection connection = sfc.createConnection();

			MessageFactory mf = MessageFactory.newInstance();
			SOAPMessage sm = mf.createMessage();

			SOAPHeader sh = sm.getSOAPHeader();
			SOAPBody sb = sm.getSOAPBody();
			sh.detachNode();
			QName bodyName = new QName(
					"http://ws.connectors.connect.mirth.com/", "acceptMessage",
					"tns");
			SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
			QName qn = new QName("arg0");
			SOAPElement quotation = bodyElement.addChildElement(qn);

			quotation.addTextNode(message);

			System.out.println("\n Soap Request:\n");
			sm.writeTo(System.out);
			System.out.println();

			URL endpoint = new URL("http://localhost:8081/services/Mirth");
			SOAPMessage response = connection.call(sm, endpoint);
			System.out.println(response.getContentDescription());
			reply = this.extractHL7Message(response.getSOAPPart().getEnvelope()
					.getBody().getTextContent());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return reply;
	}

	private String extractHL7Message(String message) {
		String hl7Message = "empty";
		String x = "<ns:return>";
		CharSequence cs = x.subSequence(0, 11);
		if (message.contains(cs)) {
			String[] firstSplit = message.split("<ns:return>");
			String[] secondSplit = firstSplit[1].split("</ns:return>");
			hl7Message = this.decodeMessage(secondSplit[0]);
		} else {
			hl7Message = this.decodeMessage(message);
		}
		return hl7Message;
	}

	private String decodeMessage(String message) {
		String step1 = message.replaceAll("&#13;", "\r");
		String step2 = step1.replaceAll("&amp;", "&");
		return step2;
	}
}
